package com.maodou.core.ui.theme

import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color

/**
 * 主题组合函数
 * 应用的主题入口点，提供完整的主题支持
 */

// ==================== CompositionLocal 定义 ====================

/**
 * 应用间距的 CompositionLocal
 */
val LocalAppSpacing = staticCompositionLocalOf<AppSpacing> {
    error("No AppSpacing provided")
}

/**
 * 应用形状的 CompositionLocal
 */
val LocalAppShapes = staticCompositionLocalOf<AppShapes> {
    error("No AppShapes provided")
}

/**
 * 主题配置的 CompositionLocal
 */
val LocalThemeConfig = staticCompositionLocalOf<ThemeConfig> {
    error("No ThemeConfig provided")
}

/**
 * 主题管理器的 CompositionLocal
 */
val LocalThemeManager = staticCompositionLocalOf<ThemeManager> {
    error("No ThemeManager provided")
}

// ==================== 主题组合函数 ====================

/**
 * 应用主题
 * 
 * @param themeManager 主题管理器，如果为 null 则使用默认单例
 * @param content 内容组合函数
 */
@Composable
fun AppTheme(
    themeManager: ThemeManager? = null,
    content: @Composable () -> Unit
) {
    val manager = themeManager ?: remember { ThemeManager.getInstance() }
    val themeConfig by manager.themeConfig.collectAsState()
    
    AppTheme(
        config = themeConfig,
        themeManager = manager,
        content = content
    )
}

/**
 * 应用主题（带配置）
 * 
 * @param config 主题配置
 * @param themeManager 主题管理器
 * @param content 内容组合函数
 */
@Composable
fun AppTheme(
    config: ThemeConfig,
    themeManager: ThemeManager = ThemeManager.getInstance(),
    content: @Composable () -> Unit
) {
    val theme = ThemeResolver.resolveTheme(config)
    
    MaterialTheme(
        colorScheme = theme.colorScheme,
        typography = theme.typography
    ) {
        CompositionLocalProvider(
            LocalAppSpacing provides theme.spacing,
            LocalAppShapes provides theme.shapes,
            LocalThemeConfig provides theme.config,
            LocalThemeManager provides themeManager,
            LocalIndication provides ripple(),
            LocalContentColor provides theme.colorScheme.onSurface
        ) {
            content()
        }
    }
}

/**
 * 预览主题
 * 用于 Compose 预览，提供默认的主题配置
 * 
 * @param darkTheme 是否为深色主题
 * @param themeType 主题类型
 * @param content 内容组合函数
 */
@Composable
fun PreviewTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    themeType: ThemeType = ThemeType.DEFAULT,
    content: @Composable () -> Unit
) {
    val config = ThemeConfig(
        themeType = themeType,
        darkModeType = if (darkTheme) DarkModeType.DARK else DarkModeType.LIGHT,
        dynamicColor = false
    )
    
    AppTheme(
        config = config,
        content = content
    )
}

// ==================== 主题访问器 ====================

/**
 * 当前主题对象
 */
object AppThemeProvider {
    
    /**
     * 获取当前间距配置
     */
    val spacing: AppSpacing
        @Composable
        get() = LocalAppSpacing.current
    
    /**
     * 获取当前形状配置
     */
    val shapes: AppShapes
        @Composable
        get() = LocalAppShapes.current
    
    /**
     * 获取当前主题配置
     */
    val config: ThemeConfig
        @Composable
        get() = LocalThemeConfig.current
    
    /**
     * 获取当前主题管理器
     */
    val manager: ThemeManager
        @Composable
        get() = LocalThemeManager.current
    
    /**
     * 获取当前颜色方案
     */
    val colorScheme
        @Composable
        get() = MaterialTheme.colorScheme
    
    /**
     * 获取当前字体排版
     */
    val typography
        @Composable
        get() = MaterialTheme.typography
    
    /**
     * 判断当前是否为深色主题
     */
    val isDark: Boolean
        @Composable
        get() = ThemeResolver.resolveDarkMode(config.darkModeType)
    
    /**
     * 判断当前是否为大屏设备
     */
    val isLargeScreen: Boolean
        @Composable
        get() = ThemeResolver.isLargeScreen()
    
    /**
     * 判断当前是否为平板设备
     */
    val isTablet: Boolean
        @Composable
        get() = ThemeResolver.isTablet()
    
    /**
     * 判断当前是否为横屏
     */
    val isLandscape: Boolean
        @Composable
        get() = ThemeResolver.isLandscape()
    
    /**
     * 判断当前是否为竖屏
     */
    val isPortrait: Boolean
        @Composable
        get() = ThemeResolver.isPortrait()
}

// ==================== 便捷扩展函数 ====================

/**
 * 获取当前自适应尺寸
 */
@Composable
fun AdaptiveDimensions.Companion.current(): AdaptiveDimensions {
    return ThemeResolver.resolveAdaptiveDimensions()
}

/**
 * 根据主题获取颜色
 */
@Composable
fun Color.onTheme(darkColor: Color): Color {
    return if (AppThemeProvider.isDark) darkColor else this
}

/**
 * 根据屏幕大小获取值
 */
@Composable
fun <T> adaptiveValue(
    compact: T,
    medium: T,
    expanded: T
): T {
    return when {
        AppThemeProvider.isTablet -> expanded
        AppThemeProvider.isLargeScreen -> medium
        else -> compact
    }
}

/**
 * 根据方向获取值
 */
@Composable
fun <T> orientationValue(
    portrait: T,
    landscape: T
): T {
    return if (AppThemeProvider.isLandscape) landscape else portrait
}

// ==================== 主题切换辅助函数 ====================

/**
 * 切换主题类型
 */
@Composable
fun switchThemeType(themeType: ThemeType) {
    AppThemeProvider.manager.updateThemeType(themeType)
}

/**
 * 切换深色模式
 */
@Composable
fun switchDarkMode(darkModeType: DarkModeType) {
    AppThemeProvider.manager.updateDarkModeType(darkModeType)
}

/**
 * 切换字体大小
 */
@Composable
fun switchFontSize(fontSizeType: FontSizeType) {
    AppThemeProvider.manager.updateFontSizeType(fontSizeType)
}

/**
 * 切换布局密度
 */
@Composable
fun switchLayoutDensity(layoutDensityType: LayoutDensityType) {
    AppThemeProvider.manager.updateLayoutDensityType(layoutDensityType)
}

/**
 * 切换动态颜色
 */
@Composable
fun toggleDynamicColor() {
    AppThemeProvider.manager.toggleDynamicColor()
}

/**
 * 切换高对比度
 */
@Composable
fun toggleHighContrast() {
    AppThemeProvider.manager.toggleHighContrast()
}

// ==================== 主题预设应用函数 ====================

/**
 * 应用主题预设
 */
@Composable
fun applyThemePreset(preset: ThemeConfig) {
    AppThemeProvider.manager.applyConfig(preset)
}

/**
 * 应用默认主题
 */
@Composable
fun applyDefaultTheme() {
    applyThemePreset(ThemePresets.Default)
}

/**
 * 应用深色主题
 */
@Composable
fun applyDarkTheme() {
    applyThemePreset(ThemePresets.Dark)
}

/**
 * 应用绿色主题
 */
@Composable
fun applyGreenTheme() {
    applyThemePreset(ThemePresets.Green)
}

/**
 * 应用紫色主题
 */
@Composable
fun applyPurpleTheme() {
    applyThemePreset(ThemePresets.Purple)
}

/**
 * 应用紧凑主题
 */
@Composable
fun applyCompactTheme() {
    applyThemePreset(ThemePresets.Compact)
}

/**
 * 应用舒适主题
 */
@Composable
fun applyComfortableTheme() {
    applyThemePreset(ThemePresets.Comfortable)
}

/**
 * 应用高对比度主题
 */
@Composable
fun applyHighContrastTheme() {
    applyThemePreset(ThemePresets.HighContrast)
}

/**
 * 应用无障碍主题
 */
@Composable
fun applyAccessibilityTheme() {
    applyThemePreset(ThemePresets.Accessibility)
}